package com.xyw.code.authentication.client.provider;

import com.xyw.code.common.core.entity.vo.Result;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created with IntelliJ IDEA.
 * User: xuyiwei
 * Date: 2020/4/22
 * Time: 下午2:27
 * Email: 1328312923@qq.com
 * Description: No Description
 **/
@Component
@FeignClient(name = "authentication-server",fallback = AuthProdiver.AuthProdiverFallback.class
)
public interface AuthProdiver {
    @PostMapping(value = "/auth/permission")
    Result auth(@RequestHeader(HttpHeaders.AUTHORIZATION) String authentication,
                @RequestParam(value = "url") String url, @RequestParam(value = "method") String method);

    @Component
    class AuthProdiverFallback implements FallbackFactory<AuthProdiver> {

        @Override
        public AuthProdiver create(Throwable throwable) {
            String msg = throwable == null ? "" : throwable.getMessage();
            if (!StringUtils.isEmpty(msg)) {
                System.out.println(msg);
            }
            return new AuthProdiver() {
                @Override
                public Result auth(String authorization, String url, String method) {
                    return Result.fail();
                }
            };
        }
    }
}
